package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.ValveCloseBean;
import com.koron.css2.clientCenter.bean.ValveCloseDetailBean;
import com.koron.css2.clientCenter.mapper.ValveCloseDetailMapper;
import com.koron.css2.clientCenter.mapper.ValveCloseMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 删除关阀信息
 *
 * @author modify by 杨嘉慧
 */
public class ValveCloseDelete implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ValveCloseDetailBean detailBean = JsonUtils.objectToPojo(req.getData(), ValveCloseDetailBean.class);
            String close_id = detailBean.getValveCloseId();

            ValveCloseMapper valveCloseMapper = factory.getMapper(ValveCloseMapper.class);
            ValveCloseBean bean = valveCloseMapper.selectByPrimaryKey(close_id);
            if (null == bean.getprocessState()) {
                //删除主表数据
                valveCloseMapper.deleteByCloseId(close_id);
                ValveCloseDetailMapper detailMapper = factory.getMapper(ValveCloseDetailMapper.class);
                //删除明细表数据
                detailMapper.delete(close_id);
                return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "删除成功", null);
            }
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "删除失败，该单据不为草稿状态", null);

        } catch (Exception e) {
            logger.error("删除失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "删除失败", null);
        }
    }
}